import React, { Component } from 'react'
import { Route, Redirect } from 'react-router-dom'
import { hasToken } from 'utils/storage'

export default class AuthRoute extends Component {
  render() {
    const { component: Component, ...rest } = this.props
    return (
      <div>
        <Route
          path={rest.path}
          render={(props) => {
            if (hasToken()) {
              return <Component {...props}></Component>
            } else {
              // 返回登录页
              return (
                <Redirect
                  to={{
                    pathname: '/login',
                    state: { pathname: props.location.pathname },
                  }}
                ></Redirect>
              )
              // props.history.push('/login')
            }
          }}
        ></Route>
      </div>
    )
  }
}
